//
//  HMMainViewController.swift
//  新浪微博-4-8v1.0
//
//  Created by 黎永锋 on 16/4/8.
//  Copyright © 2016年 黎永锋. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //get only 只读属性,不能直接使用 = 号赋值, 使用KVC
        //tabBar = HMMainTabBar()
        
        let mainTabBar = HMMainTabBar()
        
        mainTabBar.composeClosure = { () -> () in
            
            print("加号按钮被点击了")
            
        }
        
        setValue(mainTabBar, forKey: "tabBar")
        
        setupChildViewControllers()
    }

    private func setupChildViewControllers(){
    
        //首页
        setupChildViewController(HMHomeTableViewController(), imageName: "tabbar_home", title: "首页")
        
        //消息
        setupChildViewController(HMMessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        
        //发现
        setupChildViewController(HMDiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        
        //我
        setupChildViewController(HMProfileTableViewController(), imageName: "tabbar_profile", title: "我")

    }
    
    /**
     设置子控制器
     
     - parameter controller: 控制器
     - parameter imageName:  图片名称
     - parameter title:      标题
     */
    private func setupChildViewController(controller: UIViewController, imageName: String, title: String){
        
        //设置tabbar的普通图片
        controller.tabBarItem.image = UIImage(named: imageName)
        
        //设置tabbar的选中图片,拼接高亮图片名称
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置标题
        controller.title = title
        
        //设置标题颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        //包装一个导航控制器,并添加到tabBarVC里面
        addChildViewController(UINavigationController(rootViewController: controller))
    }
}
